//测试事件

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class PointerMoveIndicator extends StatefulWidget{


  @override
  State<StatefulWidget> createState() => _PointerMoveIndicatorState();
}



class _PointerMoveIndicatorState extends State{


  PointerEvent? _event;

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
        title: "手势测试",
        home: Scaffold(
          appBar: AppBar(title: Text("测试手势demo"),),
          body: Listener(
            child: Container(
              alignment: Alignment.center,
              color: Colors.blue,
              width: 300.0,
              height: 150.0,
              child: Text(
                '${_event?.localPosition ?? ''}',
                style: const TextStyle(color: Colors.white),
              ),
            ),
            onPointerDown: (PointerDownEvent event) => setState(() => _event = event),
            onPointerMove: (PointerMoveEvent event) => setState(() => _event = event),
            onPointerUp: (PointerUpEvent event) => setState(() => _event = event),
          ),
        ),
      );
  }
}

//测试手势
class  GestureTest extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => _GestureTestState();
}

class  _GestureTestState  extends State{
  String _operation = "No Gesture detected!"; //保存事件名
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "手势测试",
      home: Scaffold(
        appBar: AppBar(title: const Text("测试手势demo"),),
        body: GestureDetector(
          child: Container(
            alignment: Alignment.center,
            color: Colors.blue,
            width: 300.0,
            height: 150.0,
            child: Text(
              _operation,
              style: const TextStyle(color: Colors.white),
            ),
          ),
          onTap:() => updateText("Tap") ,
          onDoubleTap: ()=> updateText("onDoubleTap"),
          onLongPress: () => updateText("LongPress"), //长按
        ),
      ),
    );
  }
  void updateText(String text) {
    //更新显示的事件名
    setState(() {
      _operation = text;
    });
  }
}

//测试控件拖拽

class DragTest extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => _DragState();

}

  class _DragState extends State{
    double _top = 0.0; //距顶部的偏移
    double _left = 0.0;//距左边的偏移
   @override
   Widget build(BuildContext context) {

     return  MaterialApp(
       title: "手势测试",
       home: Scaffold(
         appBar: AppBar(title: const Text("测试手势demo"),),
         body: GestureDetector(
           child: Container(
             alignment: Alignment.center,
             color: Colors.blue,
             width: 300.0,
             height: 150.0,
             child:
                Text("手指滑动位置->"+_top.toString()+"---"+_left.toString(),
               style: const TextStyle(color: Colors.white),
             ) ,
           ),
            onPanDown: (DragDownDetails details){
              //打印手指按下的位置(相对于屏幕)
              print("用户手指按下：${details.globalPosition}");

            },
           // onPanUpdate: (DragUpdateDetails details){
           //   //用户手指滑动时，更新偏移，重新构建
           //   setState(() {
           //     _left += details.delta.dx;
           //     _top += details.delta.dy;
           //   });
           // },
           onPanEnd: (DragEndDetails e){
           //打印滑动结束时在x、y轴上的速度
           print(e.velocity);
         },
           onVerticalDragUpdate: (DragUpdateDetails details){
           // 只在竖直方向上滑动
             setState(() {
               _left += details.delta.dx;
               _top += details.delta.dy;
             });
           },
         ),
       ),
     );
  }
}

class PointerDownListener extends SingleChildRenderObjectWidget{

  PointerDownListener({Key? key, this.onPointerDown, Widget? child})
      : super(key: key, child: child);

  final PointerDownEventListener? onPointerDown;


  @override
  RenderObject createRenderObject(BuildContext context) =>
      RenderPointerDownListener()..onPointerDown = onPointerDown;

  @override
  void updateRenderObject(
      BuildContext context, RenderPointerDownListener renderObject) {
    renderObject.onPointerDown = onPointerDown;
  }

}

class RenderPointerDownListener extends RenderProxyBox {
  PointerDownEventListener? onPointerDown;

  @override
  bool hitTestSelf(Offset position) => true; //始终通过命中测试

  @override
  void handleEvent(PointerEvent event, covariant HitTestEntry entry) {
    //事件分发时处理事件
    if (event is PointerDownEvent) onPointerDown?.call(event);
  }
}


